package cn.piesat.scanning.service.impl;

import cn.piesat.scanning.aspect.schedulerTask.SchedulerTaskFileRecord;
import cn.piesat.scanning.dao.DbDmsScanningMetadataAuxInfoDAO;
import cn.piesat.scanning.dto.DbDmsScanningMetadataAuxInfoDTO;
import cn.piesat.scanning.service.DbDmsScanningMetadataAuxInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * 辅助数据服务层实现
 *
 * @author xhl
 * @Date 2020/8/15
 */
@Service
public class DbDmsScanningMetadataAuxInfoServiceImpl implements DbDmsScanningMetadataAuxInfoService {

    @Autowired
    private DbDmsScanningMetadataAuxInfoDAO dbDmsScanningMetadataAuxInfoDAO;

    @Autowired
    private cn.piesat.scanning.service.DbDmsSchedulerTaskFileRecordService dbDmsSchedulerTaskFileRecordService;

    /**
     * 添加辅助信息至数据库
     * @param dto 辅助信息实体类
     * @param taskId 任务ID
     */
    @Override
    public void addEntity(DbDmsScanningMetadataAuxInfoDTO dto,String taskId) {
        try {
            dbDmsScanningMetadataAuxInfoDAO.save(dto);
            dbDmsSchedulerTaskFileRecordService.dataResults(taskId,dto.getFileName(),true);
        } catch (Exception e){
            dbDmsSchedulerTaskFileRecordService.dataResults(taskId,dto.getFileName(),false);
            e.printStackTrace();
        }
    }

    @Override
    @SchedulerTaskFileRecord("dtos")
    public void addEntityBatch(List<DbDmsScanningMetadataAuxInfoDTO> dtos, String taskId) {
        if (CollectionUtils.isEmpty(dtos)) {
            return;
        }
        dbDmsScanningMetadataAuxInfoDAO.saveAll(dtos);
    }

    /**
     * 根据文件名称和文件大小查询唯一的辅助数据信息
     * @param name 文件名称
     * @param size 文件大小
     * @return
     */
    @Override
    public DbDmsScanningMetadataAuxInfoDTO queryFileUnique(String name, Long size) {
        return dbDmsScanningMetadataAuxInfoDAO.findByFileNameAndFileSize(name,size);
    }

    /**
     * 根据气象要素、时间范围查询辅助数据集合
     * @param fileWant 气象要素
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    @Override
    public List<DbDmsScanningMetadataAuxInfoDTO> listByFileWantAndRangeTime(String fileWant, String startTime, String endTime) {
        return dbDmsScanningMetadataAuxInfoDAO.findByFileWantAndRangeTime(fileWant,startTime,endTime);
    }

    @Override
    public List<DbDmsScanningMetadataAuxInfoDTO> listByFileWantListAndRangeTimeOrder(List<String> wantList, String startTime, String endTime) {
        if (wantList == null || wantList.isEmpty()) {
            return new ArrayList<>();
        }
        return dbDmsScanningMetadataAuxInfoDAO.findByFileWantListAndRangeTimeOrder(wantList,startTime,endTime);
    }

    @Override
    public Set<String> listByDate(String startTime, String endTime) {
        List<String> datalist = dbDmsScanningMetadataAuxInfoDAO.findFileNameByDate(startTime,endTime);
        return new LinkedHashSet<>(datalist);
    }


    @Override
    public List<DbDmsScanningMetadataAuxInfoDTO> getAuxFileInfoList(List<String> fileWantList, String startTime, String endTime){
        return dbDmsScanningMetadataAuxInfoDAO.findAuxFileByFileWantAndRangeTime(fileWantList,startTime,endTime);
    }

    @Override
    public List<DbDmsScanningMetadataAuxInfoDTO> findByFileCatalogAndForecastDateBetweenOrderByForecastDateDesc(String fileCatalog,  String startTime, String mainFileDate) {
        return dbDmsScanningMetadataAuxInfoDAO.findByFileCatalogAndForecastDateBetweenOrderByForecastDateDesc(fileCatalog,startTime,mainFileDate);
    }

    @Override
    public List<DbDmsScanningMetadataAuxInfoDTO> findByFileCatalogAndForecastDateOrderByFileDateDesc(String fileCatalog, String forecastDate) {
        return dbDmsScanningMetadataAuxInfoDAO.findByFileCatalogAndForecastDateOrderByFileDateDesc(fileCatalog,forecastDate);
    }
}
